package com.liao.harmony.base;

import com.liao.library.annotation.NonNull;
import com.liao.library.lifecycle.viewmodel.HarmonyViewModel;
import com.liao.library.lifecycle.viewmodel.ViewModel;
import com.liao.library.lifecycle.viewmodel.ViewModelProvider;
import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.disposables.Disposable;
import ohos.aafwk.ability.AbilityPackage;

public abstract class BaseViewModel extends HarmonyViewModel {
    protected String TAG = this.getClass().getSimpleName();
//    private CompositeDisposable mCompositeDisposable;

    public BaseViewModel(AbilityPackage mApplication) {
        super(mApplication);
    }

    public static <T extends BaseViewModel> ViewModelProvider.Factory createViewModelFactory(T viewModel) {
        return new ViewModelFactory(viewModel);
    }

    static class ViewModelFactory implements ViewModelProvider.Factory {
        BaseViewModel viewModel;
        public ViewModelFactory(BaseViewModel viewModel) {
            this.viewModel = viewModel;
        }

        @NonNull
        @Override
        public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
            return (T) viewModel;
        }
    }

//    /**
//     * 结束所有disposable
//     */
//    public void unSubscribe() {
//        if (mCompositeDisposable != null) {
//            mCompositeDisposable.dispose();
//        }
//    }
//
//    /**
//     * 移除指定disposable
//     * @param subscription
//     */
//    public void removeSubscribe(Disposable subscription) {
//        if (subscription == null || mCompositeDisposable == null) {
//            return;
//        }
//
//        mCompositeDisposable.remove(subscription);
//    }
//
//    /**
//     * 添加disposable
//     * @param subscription
//     */
//    public void addSubscribe(Disposable subscription) {
//        if (mCompositeDisposable == null) {
//            mCompositeDisposable = new CompositeDisposable();
//        }
//        mCompositeDisposable.add(subscription);
//    }
//
//    @Override
//    protected void onCleared() {
//        super.onCleared();
//        unSubscribe();
//    }
}
